//
//  ChangeNameVC.m
//  Cunli
//
//  Created by lilubin on 15/6/30.
//  Copyright (c) 2015年 lilubin. All rights reserved.
//

#import "ChangeNameVC.h"
#import "MyDataService.h"
#import "NSString+MD5.h"
#import "Config.h"
#import "User.h"

#define KCLTRUENAMECHANGE @"truenameChanged"

@interface ChangeNameVC ()
@property (weak, nonatomic) IBOutlet UITextField *nameField;

@end

@implementation ChangeNameVC

- (void)viewDidLoad {
    [super viewDidLoad];
//    if ([CLUser shareCLUser].truename != nil) {
    if([Login curLoginDetailUser].truename != nil){
//        _nameField.text = [CLUser shareCLUser].truename;
        _nameField.text = [Login curLoginDetailUser].truename;
    }
    self.navigationItem.title = @"姓名";
    [self setAllUIKitAttributes];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setAllUIKitAttributes
{
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(0, 0, 65, 30);
//    [rightBtn setBackgroundImage:[UIImage imageNamed:@"regnext"] forState:UIControlStateNormal];
    [rightBtn setTitle:@"确认" forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(commitAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    
    UIBarButtonItem *flexSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
    flexSpacer.width = flexSpacer.width - 20;
    [self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:flexSpacer, rightBarItem, nil]];
}

- (void)commitAction:(UIButton *)button
{
    [self.view endEditing:YES];

    NSMutableDictionary *CheckParams = [NSMutableDictionary dictionaryWithCapacity:4];
    [CheckParams setObject:[Config getOwnUserID] forKey:@"userid"];
    [CheckParams setObject:_nameField.text forKey:@"value"];
    [CheckParams setObject:@"truename" forKey:@"field"];
    
    NSString *resultSign = [NSString getSignFromParam:CheckParams andSession:[Config getOwnSession]];
    [CheckParams setObject:resultSign forKey:@"sign"];
    
    [MyDataService requestURL:@"edit_profile.php" httpMethod:@"POST" params:CheckParams fileDatas:nil complection:^(id result) {
        if ([result[@"response"] isEqualToString:@"edit_profile"])
        {
//            [CLUser shareCLUser].truename = _nameField.text;
            [Login curLoginDetailUser].truename = _nameField.text;
//            [Config saveUserTruename:_nameField.text];
//            [[NSNotificationCenter defaultCenter] postNotificationName:KCLTRUENAMECHANGE object:nil];
            [self.navigationController popViewControllerAnimated:YES];
        }
        else
        {
            [Config showHUDWithText:@"修改失败" andImage:@"error"];
        }
    } failureAction:^{
        [Config showHUDWithText:@"网络错误" andImage:@"error"];
    }];

}





@end
